package ylsb.controller;


import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ylsb.model.Haocaiyinjin;
import ylsb.model.Tiwaizhenduan;
import ylsb.model.YltpRenyuanfenpei;
import ylsb.model.Tiwaijieguo;
import ylsb.service.TiwaijieguoService;
import ylsb.service.TiwaizhenduanService;
import ylsb.service.YltprenyuanfenpeiService;
import ylsb.support.StringHandle;
import ylsb.service.TiwaizhenduanService;
import ylsb.support.JsonResult;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.Map;

@RestController
@RequestMapping("tiwaijieguo")
public class TiwaijieguoController {
    @Resource
    TiwaijieguoService ddService;
    @Resource
    TiwaizhenduanService ddTiwaizhenduanService;
    @Resource
    YltprenyuanfenpeiService ddYltprenyuanfenpeiService;

    public Logger log = LoggerFactory.getLogger(TiwaijieguoController.class);

    @RequestMapping("huizongbiao")
    public void huizongbiao(HttpServletRequest request, HttpServletResponse response,
                            @RequestParam("xiangmuid") String ppxiangmuid,
                            @RequestParam("xiangmuleixing") String ppxiangmuleixing){

        response.reset();
        response.setContentType("application/x-download;charset=utf-8");
        List<Map<String,Object>> mmList = ddTiwaizhenduanService.findDaochuPinzhongAll(ppxiangmuid);
        List<Map<String,Object>> mmJieGuoList=ddService.findByxiangmuId(ppxiangmuid);
        List<YltpRenyuanfenpei> mmUserList=ddYltprenyuanfenpeiService.findUserByXiangmuId(ppxiangmuid);

        // double mm=(double) mmUserList.size()*((double)2/(double)3);
        double mmNumber=Math.ceil((double) mmUserList.size()*((double)2/(double)3));
        int mmPassNumber=(int)mmNumber;
        OutputStream os=null;
        WritableWorkbook wbook=null;

        try{
            os = new BufferedOutputStream(response.getOutputStream());//得到输出流的语句
            //response.getOutputStream();// 取得输出流
            response.reset();// 清空输出流
            String wenjiantitle = "试剂表决结果汇总表"+ StringHandle.tiemString(); //文件名
            String tmptitle = "体外诊断试剂引进表决结果"; // 标题
            response.setHeader("Content-disposition", "attachment; filename="+new String(wenjiantitle.getBytes(),"iso8859-1")+".xls");// 设定输出文件头
            response.setContentType("application/vnd.ms-excel");// 定义输出类型
            wbook = Workbook.createWorkbook(os); // 建立excel文件
            WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称
            // 设置excel标题
            WritableFont wfont = new WritableFont(WritableFont.ARIAL, 20,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); //设置表格样式

            WritableCellFormat wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); // 添加边框
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            //wcfFC.setBackground(Colour.AQUA);
            Label label=new Label(0, 0, tmptitle, wcfFC);
            WritableCellFormat cellFormat=new WritableCellFormat();
            cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
            cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            cellFormat.setWrap(true);
            //cellFormat.setBackground(Colour.AQUA);
            cellFormat.setFont(wfont);
            label.setCellFormat(cellFormat);
            wsheet.addCell(label);
            wsheet.addCell(new Label(0, 0, tmptitle, wcfFC));
            /*wsheet.addCell(new Label(7, 0, tmptitle, wcfFC));*/
            wsheet.setRowView(0,500); //第一行高度
            wsheet.mergeCells(0, 0, 15, 0);  //合并单元格
            wsheet.setColumnView(0,5); //宽度
            wsheet.setColumnView(1,15); //宽度
            wsheet.setColumnView(2,15); //宽度
            wsheet.setColumnView(3,10); //宽度
            wsheet.setColumnView(4,20); //宽度
            wsheet.setColumnView(5,10); //宽度
            wsheet.setColumnView(6,10); //宽度
            wsheet.setColumnView(7,10); //宽度
            wsheet.setColumnView(8,15); //宽度
            wsheet.setColumnView(9,10); //宽度
            wsheet.setColumnView(10,10); //宽度
            wsheet.setColumnView(11,10); //宽度
            wsheet.setColumnView(12,15); //宽度
            wsheet.setColumnView(13,15); //宽度
            wsheet.setColumnView(14,20); //宽度
            wsheet.setColumnView(15,20); //宽度

            // 开始生成主体内容
            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);
            wsheet.addCell(new Label(0, 1, "序号",wcfFC));
            wsheet.addCell(new Label(1, 1, "品种",wcfFC));
            wsheet.addCell(new Label(2, 1, "品牌",wcfFC));
            wsheet.addCell(new Label(3, 1, "试剂注册证名称",wcfFC));
            wsheet.addCell(new Label(4, 1, "规格",wcfFC));
            wsheet.addCell(new Label(5, 1, "型号",wcfFC));
            wsheet.addCell(new Label(6, 1, "注册证号",wcfFC));
            wsheet.addCell(new Label(7, 1, "生产商",wcfFC));
            wsheet.addCell(new Label(8, 1, "是否封闭试剂（是/否）",wcfFC));
            wsheet.addCell(new Label(9, 1, "平台情况（江苏省/南京市/申请备案）",wcfFC));
            wsheet.addCell(new Label(10, 1, "平台编码",wcfFC));
            wsheet.addCell(new Label(11, 1, "计价单位",wcfFC));
            wsheet.addCell(new Label(12, 1, "平台单价（元）",wcfFC));
            wsheet.addCell(new Label(13, 1, "检测项目",wcfFC));
            wsheet.addCell(new Label(14, 1, "同意数／总票数",wcfFC));
            wsheet.addCell(new Label(15, 1, "结果",wcfFC));
            // 表头结束

            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12,WritableFont.NO_BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);

            int count=mmList.size();
            int mmpinzhongKaishihang=2;
            int mmpinpaiKaishihang=2;

            for(int i=0;i<count;i++){
                Map<String,Object> mmPinzhongMap=mmList.get(i);

                String mmPinzhongOne = mmPinzhongMap.get("pinzhong") == null ? "" : mmPinzhongMap.get("pinzhong").toString();
                int mmHebingshu = 0;

                List<Map<String,Object>> mmPinpailist = ddTiwaizhenduanService.findPinpaiByPinzhong(mmPinzhongOne);
                if (mmPinpailist.size()>0){
                    mmHebingshu = mmPinpailist.size()-1;
                }
                for (int j = 0; j < mmPinpailist.size(); j++) {

                    Map<String,Object> mmPinpaiMap = mmPinpailist.get(j);
                    if (mmPinpaiMap!=null){

                        String mmXuhao = mmPinpaiMap.get("ordernumber")==null ? "" :mmPinpaiMap.get("ordernumber").toString();
                        String mmpinzhong=mmPinpaiMap.get("pinzhong")==null ? "" :mmPinpaiMap.get("pinzhong").toString();
                        String mmpinpai=mmPinpaiMap.get("pinpai")==null ? "" :mmPinpaiMap.get("pinpai").toString();
                        String mmzhucezhengmingcheng=mmPinpaiMap.get("zhucezhengmingcheng")==null ? "" :mmPinpaiMap.get("zhucezhengmingcheng").toString();
                        String mmguige=mmPinpaiMap.get("guige")==null ? "" :mmPinpaiMap.get("guige").toString();
                        String mmxinghao=mmPinpaiMap.get("xinghao")==null ? "" :mmPinpaiMap.get("xinghao").toString();
                        String mmzhucezhenghao=mmPinpaiMap.get("zhucezhenghao")==null ? "" :mmPinpaiMap.get("zhucezhenghao").toString();
                        String mmshengchanshang=mmPinpaiMap.get("shengchanshang")==null ? "" :mmPinpaiMap.get("shengchanshang").toString();
                        String mmshifoufengbi=mmPinpaiMap.get("shifoufengbi")==null ? "" :mmPinpaiMap.get("shifoufengbi").toString();
                        String mmpingtaiqingkuang=mmPinpaiMap.get("pingtaiqingkuang")==null ? "" :mmPinpaiMap.get("pingtaiqingkuang").toString();
                        String mmpingtaibianma=mmPinpaiMap.get("pingtaibianma")==null ? "" :mmPinpaiMap.get("pingtaibianma").toString();
                        String mmjijiadanwei=mmPinpaiMap.get("jijiadanwei")==null ? "" :mmPinpaiMap.get("jijiadanwei").toString();
                        String mmpingtaidanjia=mmPinpaiMap.get("pingtaidanjia")==null ? "" :mmPinpaiMap.get("pingtaidanjia").toString();
                        String mmjiancexiangmu=mmPinpaiMap.get("jiancexiangmu")==null ? "" :mmPinpaiMap.get("jiancexiangmu").toString();

                        List<Map<String, Object>> mmAgreeList=mmJieGuoList.stream()
                                .filter(jieguo -> jieguo.get("xiangmuid").toString()
                                        .equals(ppxiangmuid) &&
                                        jieguo.get("pinzhong").toString()
                                                .equals(mmpinzhong)&&
                                        jieguo.get("pinpai").toString().equals(mmpinpai)&&
                                        jieguo.get("result").toString().equals("1"))
                                .collect(Collectors.toList());
                        String mmPiaoShu="";
                        String mmyijian="";
                        if(mmAgreeList==null){
                            mmPiaoShu="0／"+String.valueOf(mmUserList.size());
                            mmyijian="不通过";
                        }else {
                            mmPiaoShu=String.valueOf(mmAgreeList.size())+"／"+String.valueOf(mmUserList.size());
                            if(mmAgreeList.size()>=mmPassNumber){
                                mmyijian="通过";
                            }else{
                                mmyijian="不通过";
                            }
                        }

                        wsheet.addCell(new Label(0, mmpinpaiKaishihang,mmXuhao,wcfFC));
                        wsheet.addCell(new Label(2, mmpinpaiKaishihang,mmpinpai,wcfFC));
                        wsheet.addCell(new Label(3, mmpinpaiKaishihang,mmzhucezhengmingcheng,wcfFC));
                        wsheet.addCell(new Label(4, mmpinpaiKaishihang,mmguige,wcfFC));
                        wsheet.addCell(new Label(5, mmpinpaiKaishihang,mmxinghao,wcfFC));
                        wsheet.addCell(new Label(6, mmpinpaiKaishihang,mmzhucezhenghao,wcfFC));
                        wsheet.addCell(new Label(7, mmpinpaiKaishihang,mmshengchanshang,wcfFC));
                        wsheet.addCell(new Label(8, mmpinpaiKaishihang,mmshifoufengbi,wcfFC));
                        wsheet.addCell(new Label(9, mmpinpaiKaishihang,mmpingtaiqingkuang,wcfFC));
                        wsheet.addCell(new Label(10, mmpinpaiKaishihang,mmpingtaibianma,wcfFC));
                        wsheet.addCell(new Label(11, mmpinpaiKaishihang,mmjijiadanwei,wcfFC));
                        wsheet.addCell(new Label(12, mmpinpaiKaishihang,mmpingtaidanjia,wcfFC));
                        wsheet.addCell(new Label(13, mmpinpaiKaishihang,mmjiancexiangmu,wcfFC));
                        wsheet.addCell(new Label(14, mmpinpaiKaishihang,mmPiaoShu,wcfFC));
                        wsheet.addCell(new Label(15, mmpinpaiKaishihang,mmyijian,wcfFC));
                        mmpinpaiKaishihang = mmpinpaiKaishihang+1;
                    }
                }
                wsheet.addCell(new Label(1, mmpinzhongKaishihang, mmPinzhongOne,wcfFC));
                wsheet.mergeCells(1, mmpinzhongKaishihang, 1, mmpinzhongKaishihang+mmHebingshu);
                mmpinzhongKaishihang = mmpinzhongKaishihang+mmHebingshu+1;

            }
            wbook.write(); // 写入文件
            wbook.close();





        }catch(Exception ex)
        {
            ex.printStackTrace();
        }finally{
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

    @RequestMapping("lingdaohuibaobiao")
    public void lingdaohuibaobiao(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam("xiangmuid") String ppxiangmuid,
                                  @RequestParam("xiangmuleixing") String ppxiangmuleixing) {
        // List<Map<String,Object>> mmJieGuoList=ddService.findByxiangmuId(ppxiangmuid);

        List<List<String>> mmResultList = new ArrayList<List<String>>();
        List<YltpRenyuanfenpei> mmUserList=ddYltprenyuanfenpeiService.findUserByXiangmuId(ppxiangmuid);
        int mmWeiyuanshu=mmUserList.size();
        for(int i=0;i<mmUserList.size();i++){
            Integer mmSubmit = mmUserList.get(i).getIssubmit();
            if(mmSubmit==0){
                error("该项目有委员没有提交结果表",response);
                return;
            }
        }
        double mmNumber=Math.ceil((double) mmUserList.size()*((double)2/(double)3));
        int mmPassNumber=(int)mmNumber;
        List<Map<String,Object>> mmList = ddTiwaizhenduanService.findDaochuPinzhongAll(ppxiangmuid);
        int mmPinzhongShu=mmList.size();
        List<Tiwaizhenduan> mmTiwaizhenduanList=ddTiwaizhenduanService.findAllByXiangmuId(ppxiangmuid);
        int mmPingGui=mmTiwaizhenduanList.size();

        int mmPassPingGui=0;
        int mmNoPassPingGui=0;

        List<Map<String,Object>> mmPinguiList=ddTiwaizhenduanService.findPinguiResult(ppxiangmuid);
        for(Tiwaizhenduan mmTiwaizhenduan : mmTiwaizhenduanList){

            List<Map<String, Object>> mmAgreeList=mmPinguiList.stream()
                    .filter(pingui -> pingui.get("id").toString()
                            .equals(mmTiwaizhenduan.getId()) &&
                            pingui.get("result").toString().equals("1"))
                    .collect(Collectors.toList());
            if(mmAgreeList==null){
                mmNoPassPingGui=mmPingGui;
            }else {

                if(mmAgreeList.size()>=mmPassNumber){
                    mmPassPingGui=mmPassPingGui+1;
                }else{
                    mmNoPassPingGui=mmNoPassPingGui+1;
                }
            }
        }
        String mmMeg="今天共计"+mmWeiyuanshu+"个委员参与体外诊断试剂引进表决，同意数在"+mmPassNumber+"票及以上，最终结果为通过。体外诊断试剂投票共计"
                +mmPinzhongShu+"个品种，涉及"+mmPingGui+"个品规，其中"+mmPassPingGui+"个品规同意数在"+mmPassNumber+"票及以上，最终通过；"
                +mmNoPassPingGui+"个品规同意数不足"+mmPassNumber+"票，最终结果不通过。";
       /* List<String> mmTitleList = new ArrayList<String>();
        mmTitleList.add(mmMeg);
        mmResultList.add(mmTitleList);
        try {
            //取出文档存放的根目录，并去除文件最后的斜杠
            if (userfilepath.endsWith("/")) {
                userfilepath = userfilepath.substring(0, userfilepath.length()-1);
            }

            SimpleDateFormat mmDateFormat = new SimpleDateFormat("yyyyMMdd");
            String mmSavePath = userfilepath +  "/temp/tiwaijieguo/"+mmDateFormat.format(new Date()) + "/";

            CreateXLS.createExcel(mmResultList, mmSavePath,  "hbb"+ UUID.randomUUID().toString()+".docx",
                    "hbb", request, response);
            return "/ylsbtp/lingdaohuibaobiao.docx";

        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return e.getMessage();
        }*/

        response.reset();
        response.setContentType("application/x-download;charset=utf-8");
        OutputStream os=null;
        WritableWorkbook wbook=null;

        try{
            os = new BufferedOutputStream(response.getOutputStream());//得到输出流的语句
            //response.getOutputStream();// 取得输出流
            response.reset();// 清空输出流
            String wenjiantitle = "体外诊断试剂引进表决结果汇报表"+ StringHandle.tiemString(); //文件名
            String tmptitle = "体外诊断试剂引进表决结果汇报表"; // 标题
            response.setHeader("Content-disposition", "attachment; filename="+new String(wenjiantitle.getBytes(),"iso8859-1")+".xls");// 设定输出文件头
            response.setContentType("application/vnd.ms-excel");// 定义输出类型
            wbook = Workbook.createWorkbook(os); // 建立excel文件
            WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称
            // 设置excel标题
            WritableFont wfont = new WritableFont(WritableFont.ARIAL, 20,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); //设置表格样式

            WritableCellFormat wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); // 添加边框
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            //wcfFC.setBackground(Colour.AQUA);
            Label label=new Label(0, 0, tmptitle, wcfFC);
            WritableCellFormat cellFormat=new WritableCellFormat();
            cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
            cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            cellFormat.setWrap(true);
            //cellFormat.setBackground(Colour.AQUA);
            cellFormat.setFont(wfont);
            label.setCellFormat(cellFormat);
            wsheet.addCell(label);
            wsheet.addCell(new Label(0, 0, tmptitle, wcfFC));
            /*wsheet.addCell(new Label(7, 0, tmptitle, wcfFC));*/
            wsheet.setRowView(0,500); //第一行高度
            wsheet.mergeCells(0, 0, 10, 0);  //合并单元格
            wsheet.setColumnView(0,5); //宽度
            wsheet.setColumnView(1,15); //宽度
            // 开始生成主体内容
            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);

            // 表头结束

            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12,WritableFont.NO_BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);

            wsheet.addCell(new Label(0, 1, mmMeg,wcfFC));
            wsheet.mergeCells(0, 1, 10, 6);
            wbook.write(); // 写入文件
            wbook.close();


        }catch(Exception ex)
        {
            ex.printStackTrace();
        }finally{
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }



    }
    public void error(String string,HttpServletResponse mmResponse) {
        try {
            mmResponse.setCharacterEncoding("gbk");
            PrintWriter mmOut;
            mmOut = mmResponse.getWriter();
            mmOut.print("<div style='text-align:center;font-size:18px; font-weight:weight;'>"+string+"</div>");
            mmOut.flush();
            mmOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 导出某个人员的签字表
    @RequestMapping("qianzibiao")
    public void qianzibiao(HttpServletRequest request, HttpServletResponse response,
                           @RequestParam("xiangmuid") String ppxiangmuid,
                           @RequestParam("xiangmuleixing") String ppxiangmuleixing,
                           @RequestParam("userid") String ppuserid) {
        try {
            ddService.daochuqianzibiao(ppxiangmuid, ppuserid, request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("toupiao")
    public JsonResult<?> toupiao(@RequestParam("id") String ppdetailId,
                                 @RequestParam("pinzhong") int ppPinzhong,
                                 @RequestParam("pinpai") String ppPinpai,
                                 @RequestParam("userid") String ppuserId,
                                 @RequestParam("result") String ppresult){
        try {
            Tiwaijieguo mmTiwaijieguo=new Tiwaijieguo();
            if("1".equals(ppresult)) {
                if (ddService.checkCountByPinzhongandUserId(ppPinzhong, ppuserId) < 2) {
                    if (ddService.checkexistByUserId(ppPinzhong, ppPinpai, ppuserId) != null) {
                        String mmId = ddService.checkexistByUserId(ppPinzhong, ppPinpai, ppuserId).getId();
                        mmTiwaijieguo.setId(mmId);
                        mmTiwaijieguo.setResult(ppresult);
                        mmTiwaijieguo.setCreatetime(new Date());
                        return ddService.modify(mmTiwaijieguo) == true ? JsonResult.getSuccessResult("表决成功")
                                : JsonResult.getErrorResult("表决失败");
                    }else {
                        mmTiwaijieguo.setId(UUID.randomUUID().toString());
                        mmTiwaijieguo.setDetailid(ppdetailId);
                        mmTiwaijieguo.setPinzhong(ppPinzhong);
                        mmTiwaijieguo.setPinpai(ppPinpai);
                        mmTiwaijieguo.setUserid(ppuserId);
                        mmTiwaijieguo.setResult(ppresult);
                        mmTiwaijieguo.setCreatetime(new Date());
                        return ddService.add(mmTiwaijieguo) == true ? JsonResult.getSuccessResult("表决成功")
                                : JsonResult.getErrorResult("表决失败");
                    }

                } else {
                    return JsonResult.getErrorResult("同一品种的品牌同意数量不能超过2条。");
                }
            }else {
                if (ddService.checkexistByUserId(ppPinzhong, ppPinpai, ppuserId) != null) {
                    String mmId = ddService.checkexistByUserId(ppPinzhong, ppPinpai, ppuserId).getId();
                    mmTiwaijieguo.setId(mmId);
                    mmTiwaijieguo.setResult(ppresult);
                    mmTiwaijieguo.setCreatetime(new Date());
                    return ddService.modify(mmTiwaijieguo) == true ? JsonResult.getSuccessResult("表决成功")
                            : JsonResult.getErrorResult("表决失败");
                } else {
                    mmTiwaijieguo.setId(UUID.randomUUID().toString());
                    mmTiwaijieguo.setDetailid(ppdetailId);
                    mmTiwaijieguo.setPinzhong(ppPinzhong);
                    mmTiwaijieguo.setPinpai(ppPinpai);
                    mmTiwaijieguo.setUserid(ppuserId);
                    mmTiwaijieguo.setResult(ppresult);
                    mmTiwaijieguo.setCreatetime(new Date());
                    return ddService.add(mmTiwaijieguo) == true ? JsonResult.getSuccessResult("表决成功")
                            : JsonResult.getErrorResult("表决失败");
                }

            }
        }catch (Exception e) {
            e.printStackTrace();
            log.error("tiwaijieguo/toupiao:error",e);
            return JsonResult.getErrorResult("tiwaijieguo/toupiao:error " + e.getMessage());
        }

    }

    @RequestMapping("getTiWaiResultCount")
    public JsonResult<?> getTiWaiResultCount(@RequestParam("xiangmuid") String ppxiangmuId,
                                                     @RequestParam("userid") String ppuserId){
        try {
            Integer mmTongyishu=ddService.getTiWaiResultCount(ppxiangmuId,ppuserId,"1");
            Integer mmButongyishu=ddService.getTiWaiResultCount(ppxiangmuId,ppuserId,"0");
            Integer mmTotal=ddTiwaizhenduanService.getVoteCount(ppxiangmuId);
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("tongyiShu", mmTongyishu);
            mmMap.put("butongyiShu", mmButongyishu);
            mmMap.put("total", mmTotal);
            return JsonResult.getSuccessResult(mmMap);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("tiwaijieguo/getTiWaiResultCount:error",e);
            return JsonResult.getErrorResult("tiwaijieguo/getTiWaiResultCount:error " + e.getMessage());
        }
    }





}
